package com.may.wlrj.services;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;

public class RabbitConsumer implements ChannelAwareMessageListener {
	private GateWayService gatewayService;
    @Autowired
    public void setGatewayService(GateWayService gatewayService){
        this.gatewayService = gatewayService;
    }
	private ObjectMapper mapper; 
    private com.fasterxml.jackson.databind.JsonNode rootNode;
    
	@Override
    public void onMessage(Message message, Channel channel) throws Exception {
        byte[] body = message.getBody();
        mapper = new ObjectMapper(); 
        
		rootNode = mapper.readTree(new String(body));
		try {
			String reqtype = rootNode.get("type").asText();
			
			
			
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		
        //System.out.println("1  receive msg : " + JSONObject.parseObject(new String(body)));
        //不读取消息并且将当前消息抛弃掉，消息队列中删除当前消息
        //channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        //不读取消息，消息队列中保留当前消息未被查看状态
        //channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);

        //确认消息成功消费，删除消息队列中的消息
       // channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        //确认消息成功消费，删除消息队列中的消息，他跟上面貌似一样
         
    }

}
